About: this statistics are compilated with Sidewalks,Crossings and Kerbs data.
All the code is kept here, so anyone can reproduce!
Scroll down and the charts will begin to appear, they we're made with the amazing Altair library, that enables interactivity
Sidewalks Statistics
Crossings Statistics
Kerbs Statistics
currently it's only optimized for desktop
from datetime import datetime
now = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print('Last Update: ',dt_string)
Last Update: 05/07/2022 13:11:49
import geopandas as gpd
import pandas as pd
import altair as alt
def get_count_df(input_df,fieldname,str_to_append=' type'):
outfieldname = fieldname+str_to_append
return input_df[fieldname].value_counts().reset_index().rename(columns={'index':outfieldname,fieldname:'count'}).sort_values(by='count',ascending=False),outfieldname
def create_barchart(input_df,fieldname,title,str_to_append=' type',title_fontsize=24,tooltip='count',x_sort='-y',tooltip_list=['percent']):
# bind = alt.selection_interval(bind='scales')
# .add_selection(bind)
data_to_plot,fieldname_v2 = get_count_df(input_df,fieldname,str_to_append)
feat_count = float(data_to_plot['count'].sum())
def compute_formatted_percent(featureval):
return str(round((featureval/feat_count)*100,2))+"%"
data_to_plot['percent'] = data_to_plot['count'].apply(compute_formatted_percent)
return alt.Chart(data_to_plot,title=title).mark_bar().encode(
x=alt.X(fieldname_v2,sort=x_sort),
y='count',
tooltip=tooltip_list,
).properties(
width=650,
height=300).configure_title(fontSize=title_fontsize).interactive()
def create_barchartV2(input_gdf,fieldname,title,str_to_append=' type',title_fontsize=24,len_field='length(km)'):
# bind = alt.selection_interval(bind='scales')
# .add_selection(bind)
fieldname_v2 = fieldname+str_to_append
data_to_plot = input_gdf[[len_field,fieldname]].groupby([fieldname]).agg({fieldname:'count',len_field:'sum'}).rename(columns={fieldname:'feature count'}).reset_index().rename(columns={fieldname:fieldname_v2})
return alt.Chart(data_to_plot,title=title).mark_bar().encode(
x=alt.X(fieldname_v2,sort='-y'),
y=len_field,
tooltip=len_field,
color='feature count'
).properties(
width=650,
height=300).configure_title(fontSize=title_fontsize).interactive()
def print_relevant_columnames(input_df,not_include=('score','geometry','type','id')):
print(*[f'{column}, ' for column in input_df.columns if not any(word in column for word in not_include)])
def return_weblink(string_id,type='way'):
return f"<a href=https://www.openstreetmap.org/{type}/{string_id}>{string_id}</a>"
def get_year_surveydate(featuredate):
return featuredate.split('-')[0]
sidewalks_gdf = gpd.read_file('../data/sidewalks.geojson')
sidewalks_data = pd.DataFrame(sidewalks_gdf)
# compute lengths only once:
sidewalks_gdf['length(km)'] = sidewalks_gdf.to_crs('EPSG:31982').length/1000
sidewalks_gdf['weblink'] = sidewalks_gdf['id'].astype('string').apply(return_weblink)
sidewalks_gdf['Year of Survey'] = sidewalks_gdf['survey:date'].apply(get_year_surveydate)
# sidewalk Length Statistics
sidewalks_gdf['length(km)'].describe()
count 1990.000000 mean 0.080533 std 0.158999 min 0.000935 25% 0.012500 50% 0.025342 75% 0.064382 max 2.852458 Name: length(km), dtype: float64
printing relevant columns on the data:
print_relevant_columnames(sidewalks_gdf)
bicycle, footway, highway, name, foot, lcn, motor_vehicle, segregated, access, horse, oneway, mapillary, maxspeed, survey:date, tactile_paving, layer, lit, surface, tunnel, incline, smoothness, opening_hours, cutting, embankment, dog, wheelchair, level, cycleway, cycleway:right, ramp, noname, crossing, alt_name, source, handrail, ramp:wheelchair, step_count, kerb, traffic_signals, description, paving_stones, barrier, incline:across, length(km), weblink, Year of Survey,
create_barchartV2(sidewalks_data,'surface','Sidewalks Surface Type',title_fontsize=24)
create_barchartV2(sidewalks_data,'smoothness','Sidewalks Smoothness Level',title_fontsize=24)
create_barchartV2(sidewalks_data,'tactile_paving','Sidewalks Tactile Paving Presence',title_fontsize=24)
create_barchartV2(sidewalks_data,'width','Sidewalks Width Values',title_fontsize=24)
create_barchartV2(sidewalks_data,'incline','Sidewalks Incline Values',title_fontsize=24)
def double_scatter_bar(input_df,title,xs='surface',ys='smoothness',scolor=None,xh='count()',yh1='surface',yh2='smoothness',hcolor=None,fontsize=24,tooltip_fields=['type','id']):
interval = alt.selection_interval()
default_color = alt.value('lightseagreen')
if not hcolor:
hcolor = default_color
if not scolor:
scolor = default_color
scatter = alt.Chart(input_df,title=title).mark_point().encode(
x=xs,
y=ys,
color=scolor,
tooltip=alt.Tooltip(tooltip_fields),
).properties(
width=600,
height=350,).add_selection(interval)
hist_base = alt.Chart(input_df).mark_bar().encode(
x=xh,
color=hcolor,
tooltip=alt.Tooltip(tooltip_fields),
).properties(
width=300,
height=220,
).transform_filter(
interval,
)
# if hcolor:
# hist_base.encode(color=hcolor)
hist = hist_base.encode(y=yh1) | hist_base.encode(y=yh2)
return (scatter & hist).configure_title(fontSize=fontsize,align='center')
# 'Surface x Smoothness'
double_scatter_bar(sidewalks_gdf,'Surface x Smoothness (sidewalks)',hcolor='length(km)')
create_barchart(sidewalks_gdf,'Year of Survey','Year of Survey Image (sidewalks)')
# updating info:
sidewalks_updating = pd.read_json('../data/sidewalks_versioning.json')
crossings_updating = pd.read_json('../data/crossings_versioning.json')
kerbs_updating = pd.read_json('../data/kerbs_versioning.json')
updating_dict = {'sidewalks':sidewalks_updating,'crossings':crossings_updating,'kerbs':kerbs_updating}
# crating month_year field:
for category in updating_dict:
updating_dict[category]['month_year'] = updating_dict[category]['rev_month'].map("{:02d}".format) + '_' + updating_dict[category]['rev_year'].astype(str)
updating_dict[category]['year_month'] = updating_dict[category]['rev_year'].astype(str) + "_" + updating_dict[category]['rev_month'].map("{:02d}".format)
updating_dict[category].sort_values('year_month',inplace=True)
print_relevant_columnames(sidewalks_updating,not_include=[])
osmid, rev_day, rev_month, rev_year, n_revs, month_year, year_month,
create_barchart(sidewalks_updating,'year_month','Year and Month Of Update (Sidewalks)',x_sort='-x')
create_barchart(sidewalks_updating,'n_revs','Number Of Revisions (Sidewalks)',x_sort='-x')
crossings_gdf = gpd.read_file('../data/crossings.geojson')
crossings_data = pd.DataFrame(crossings_gdf)
# compute lengths only once:
crossings_gdf['length(km)'] = crossings_gdf.to_crs('EPSG:31982').length/1000
crossings_gdf['weblink'] = crossings_gdf['id'].astype('string').apply(return_weblink)
crossings_gdf['Year of Survey'] = crossings_gdf['survey:date'].apply(get_year_surveydate)
print_relevant_columnames(crossings_gdf)
crossing, footway, highway, kerb, surface, tactile_paving, traffic_calming, bicycle, name, foot, lit, segregated, layer, access, alt_name, horse, note, motor_vehicle, incline, lcn, crossing:island, lanes, oneway, level, cycleway, cycleway:right, smoothness, wheelchair, mapillary, survey:date, barrier, length(km), weblink, Year of Survey,
create_barchart(crossings_gdf,'crossing','Crossing Type')
create_barchart(crossings_gdf,'surface','Crossing Surface')
double_scatter_bar(crossings_gdf,'Surface x Smoothness (crossings)',
hcolor='crossing',
# scolor='crossing',
)
create_barchart(crossings_gdf,'Year of Survey','Year of Survey Image (crossings)')
create_barchart(crossings_updating,'year_month','Year and Month Of Update (Crossings)',x_sort='-x')
create_barchart(crossings_updating,'n_revs','Number Of Revisions (Crossings)',x_sort='-x')
kerbs_gdf = gpd.read_file('../data/kerbs.geojson')
kerbs_data = pd.DataFrame(kerbs_gdf)
kerbs_gdf['Year of Survey'] = kerbs_gdf['survey:date'].apply(get_year_surveydate)
print_relevant_columnames(kerbs_gdf)
crossing, crossing_ref, highway, kerb, tactile_paving, traffic_calming, bicycle, mapillary, survey:date, wheelchair, button_operated, traffic_signals:sound, traffic_signals:vibration, crossing:island, image, barrier, surface, kerb:height, traffic_signals, smoothness, description, Year of Survey,
create_barchart(kerbs_gdf,'kerb','Kerb Type')
create_barchart(kerbs_gdf,'tactile_paving','Kerb Tactile Paving Presence')
create_barchart(kerbs_gdf,'wheelchair','Kerb Wheelchair Acessibility')
create_barchart(kerbs_gdf,'kerb:height','Kerb Height')
create_barchart(kerbs_gdf,'Year of Survey','Year of Survey Image (kerbs)')
double_scatter_bar(kerbs_gdf,' ',xs='kerb',ys='tactile_paving',yh1='kerb',yh2='tactile_paving',xh='count()',hcolor='wheelchair')
create_barchart(kerbs_updating,'year_month','Year and Month Of Update (Kerbs)',x_sort='-x')
create_barchart(kerbs_updating,'n_revs','Number Of Revisions (Kerbs)',x_sort='-x')
import sys
sys.path.append('..')
from time import sleep
from functions import record_datetime,gen_updating_infotable_page
# to record data aging:
record_datetime('Statistical Charts','../data/last_updated.json')
sleep(.1)
# generate the "report" of the updating info
gen_updating_infotable_page('../data/data_updating.html','../data/last_updated.json')
!jupyter nbconvert --to html statistics.ipynb
[NbConvertApp] Converting notebook statistics.ipynb to html [NbConvertApp] Writing 4131388 bytes to statistics.html